package com.axdoc.wallet.service.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.axdoc.common.constant.WalletConstant;
import com.axdoc.common.utils.SnowflakeIdWorker;
import com.axdoc.wallet.dao.IDoctorWalletDao;
import com.axdoc.wallet.entity.DoctorBalanceSubWallet;
import com.axdoc.wallet.entity.DoctorBalanceSubWalletRecords;
import com.axdoc.wallet.entity.DoctorWallet;
import com.axdoc.wallet.exceptions.WalletBizException;
import com.axdoc.wallet.service.DoctorWalletService;

@Service("doctorWalletService")
@Path("doctor")
@Produces({ "application/json; charset=UTF-8", "text/xml; charset=UTF-8" })
public class DoctorWalletServiceImpl implements DoctorWalletService {

	@Autowired
	private IDoctorWalletDao doctorWalletDao;

	@GET
	@POST
	@Path("/createDoctorWallet/")
	@Transactional(rollbackFor = Exception.class)
	public boolean createDoctorWallet(@QueryParam("docid") String docid, @QueryParam("walletType") int walletType)
			throws WalletBizException, Exception {
		boolean result = false;
		if (StringUtils.isBlank(docid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象docid");
		}
		if (0 != walletType && 1 != walletType) {
			throw new WalletBizException(WalletBizException.NOT_EXSIT, "参数对象walletType值不正确");
		}
		String walletNo = String.valueOf(SnowflakeIdWorker.getInstance().nextId());
		if (StringUtils.isBlank(walletNo)) {
			throw new WalletBizException(WalletBizException.ERROR, "医生账户编号生成失败！不能为空！");
		}
		// 添加医生账户
		DoctorWallet dw = new DoctorWallet();
		dw.setWalletNo(walletNo);
		dw.setDocid(docid);
		dw.setWalletType(walletType);
		dw.setpNo("");
		result = doctorWalletDao.addDoctorWallet(dw);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "医生账户添加失败！");
		}
		// 添加医生余额子账户
		DoctorBalanceSubWallet balance = new DoctorBalanceSubWallet();
		balance.setWalletNo(walletNo + "01");
		balance.setmWalletNo(walletNo);
		balance.setPayPassword("");
		balance.setIsAllowedPay(WalletConstant.WALLET_IS_ALLOW_PAY);
		balance.setIsAllowedRecharge(WalletConstant.WALLET_IS_ALLOW_RECHARGE);
		balance.setIsAllowedWithdrawal(WalletConstant.WALLET_IS_ALLOW_WITHDRAWL);
		balance.setState(WalletConstant.WALLET_STATUS_ALIVE);
		balance.setBalance(0);
		balance.setWithdrawalBalanceCan(0);
		balance.setWithdrawalBalanceWait(0);
		balance.setForzenBalance(0);
		balance.setDocid(docid);
		result = doctorWalletDao.addDoctorBalanceSubWallet(balance);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "医生余额子账户添加失败！");
		}
		return result;
	}

	@GET
	@POST
	@Path("/queryDoctorWalletByDocid/")
	public DoctorWallet queryDoctorWalletByDocid(@QueryParam("docid") String docid)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(docid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象docid");
		}
		return doctorWalletDao.queryDoctorWalletByDocid(docid);
	}

	@GET
	@POST
	@Path("/queryDoctorBalanceSubWalletByDocid/")
	public DoctorBalanceSubWallet queryDoctorBalanceSubWalletByDocid(@QueryParam("docid") String docid)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(docid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象docid");
		}
		return doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
	}

	@POST
	@Path("/updateDoctorBalanceSubWallet/")
	@Consumes({ MediaType.APPLICATION_JSON })
	public boolean updateDoctorBalanceSubWallet(DoctorBalanceSubWallet doctorBalanceSubWallet)
			throws WalletBizException, Exception {
		if (null == doctorBalanceSubWallet || 0 == doctorBalanceSubWallet.getId()) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象id");
		}
		return doctorWalletDao.updateDoctorBalanceSubWallet(doctorBalanceSubWallet);
	}

	@GET
	@POST
	@Path("/queryBalanceRecords/")
	public List<DoctorBalanceSubWalletRecords> queryBalanceRecords(@QueryParam("bizNo") String bizNo,
			@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(bizNo)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象bizNo");
		}
		if (StringUtils.isBlank(startTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象startTime");
		}
		if (StringUtils.isBlank(endTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象endTime");
		}
		Timestamp st = Timestamp.valueOf(startTime);
		Timestamp et = Timestamp.valueOf(endTime);
		return doctorWalletDao.queryBalanceRecords(bizNo, st, et);
	}
	
	

}
